package com.fingard.dsp.bank.directbank.qdcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class QDCB01Balance extends QDCB01Base implements IBalance {

    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            while (p_req7506.readRecord()) {//单笔余额查询
                bsnCode = "CBE001";
                serialNo = p_req7506.reqHeader.transSeqID;
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter);
                xmlWriter.writeStartElement("ReqParam");
                xmlWriter.writeElementString("accountNo", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("currencyType", convertCurToBank(p_req7506.readedDetail.actCur));
                //先默认为实际账户吧
                String accountType = convertAccountTypeToBank(p_req7506.readedDetail.savingType);
                if(accountType==null||accountType.equals("")){
                    xmlWriter.writeElementString("accountType", "D");
                }else {
                    xmlWriter.writeElementString("accountType", accountType);
                }//账户类型  D：结算账户S：储蓄账户 默认为D
                xmlWriter.writeElementString("ReqReserved1", "", true);
                xmlWriter.writeElementString("ReqReserved2", "", true);
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = null;
                    String tmpStatus = tmpStrRet[1].substring(0, tmpStrRet[1].indexOf("|", 0));
                    String tmpStatusText = tmpStrRet[1].substring(tmpStrRet[1].indexOf("|", 0) + 1, tmpStrRet[1].lastIndexOf("|"));
                    if (tmpStatus != null && isActiveRetCode(tmpStatus)) {
                        tmpBkReader = loadBankRespXmlReader(tmpStrRet[1].substring(tmpStrRet[1].indexOf("<")), tmpSbMsg);
                        if (tmpBkReader != null) {
                            while (tmpBkReader.read("/QDBankData/opRep/opResult")) {
                                if (!tmpBkReader.readedXElement.getSingleText("accountState").equals("2")) {//accStatus 1-正常、2-销户、3-新开户（未启用单位户有效）
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("FreeBalance");
                                    tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("CurrentBalance");
                                } else {
                                    tmpRetDetail.respCode = "-1";
                                    tmpRetDetail.respInfo = "账户已销户";
                                }
                            }
                        }else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpSbMsg.toString();
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpStatusText;
                    }
            } else{
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
        }
    } catch(
    Exception e)

    {
        retAts.writeRespCode("-1");
        retAts.writeRespInfo(e.getMessage());
        e.printStackTrace();
    }
        retAts.writeEnd();
        return retAts;
}

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }

}
